import { _decorator, Node, ProgressBar, Label, find, v3, lerp } from 'cc';
import { BasePanel } from '../../../../scripts/libs/ui/BasePanel';
import { gm } from '../../../../scripts/managers/GameManager';
import { LaunchConfig } from '../../../../Launch/LaunchConfig';
import { TweenToStart } from '../../../../scripts/libs/rxjs/cc3/TweenStart';
import { instantFunc } from '../../../../scripts/libs/rxjs/cc3/CommonObservable';
import { Observable, of } from 'rxjs';
import { LoadingPanelMode } from '../../../../scripts/managers/Enums';
import { MergeASAP } from '../../../../scripts/libs/rxjs/cc3/Merge';
const { ccclass, property } = _decorator;

const _v30 = v3();

@ccclass('LoadingPanel')
export class LoadingPanel extends BasePanel {

    /**
     * 使用时，先设定Mode，一般为 LoadingPanelMode.Default(从0~100%)
     * 修改gm.maxProgressValue,再设定gm.curProgressValue.value即可观察到变化
     */

    @property(Node)
    bgNode: Node = null;

    @property(ProgressBar)
    progressBar: ProgressBar = null;

    crabNode: Node = null;
    crabLabel: Label = null;

    willBind() {
        this.crabNode = find('crab', this.progressBar.node);
        this.crabLabel = find('Label', this.crabNode).getComponent(Label);
    }

    useObserves() {
        return [
            MergeASAP(gm.curProgressValue, gm.curProgressValue2).subscribe(this.refresh.bind(this)),
        ];
    }

    refresh() {
        const v = Math.min(gm.curProgressValue.value + gm.curProgressValue2.value, gm.maxProgressValue);
        let progressValue = 0;
        switch (gm.loadingPanelMode) {
            case LoadingPanelMode.FirstTime: {
                // this.progressLabel.string = `加载中... ${Math.round(v / gm.maxProgressValue * 100 * (1 - LaunchConfig.launch.firstProgressRateMax) + (LaunchConfig.launch.firstProgressRateMax * 100))}%`;
                progressValue = v / gm.maxProgressValue * (1 - LaunchConfig.launch.firstProgressRateMax) + LaunchConfig.launch.firstProgressRateMax;
                this.progressBar.progress = progressValue;
                break;
            }
            case LoadingPanelMode.Default: {
                // this.progressLabel.string = `加载中... ${Math.round(v / gm.maxProgressValue * 100)}%`;
                progressValue = v / gm.maxProgressValue;
                this.progressBar.progress = progressValue;
                break;
            }
        }

        this.crabNode.getPosition(_v30);
        _v30.x = lerp(-410, 410, progressValue);
        this.crabNode.setPosition(_v30);
        this.crabLabel.string = `${Math.round(progressValue * 100)}%`;
        this.log('@@this.crabLabel.string', this.crabLabel.string);
    }

    static openCount = 0;

    afterOpen() {
        LoadingPanel.openCount++;
        return of('afterOpen');
    }

    openPanelAnim(): Observable<any> {
        if (LoadingPanel.openCount == 0) {
            return instantFunc(() => this.ensureUIOpacity().opacity = 255);
        } else {
            return TweenToStart(this.ensureUIOpacity(), .2, { opacity: 255 }, { easing: 'expoOut' });
        }
    }

}


